<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    //
    public function getAdd()
    {
    	$data = DB::table('cate')
        ->select('*',DB::raw("CONCAT(path,',',id) AS sortPath"))
        ->orderby('sortPath')
        ->get();

        foreach($data as $k=>$v)
        {   //获取path里','的个数
            //substr_repeat重复一个字符串
            $num = substr_count($v ->path,',');

            //利用','的个数去区分分类名
            $data[$k] -> name = str_repeat('|----', $num).$v ->name;
        }
       
    	return  view('admin.goods.add',['data' => $data]);
    }

    public function postInsert(Request $request)
    {
    	$data = $request ->except('_token');

    	// 上传图片
    	
    	if($request -> hasFile('img'))
    	{
    		 
    		if($request ->file('img') -> isValid())
    		{
    			//移动文件
    			//创建文件名称
    			//获取文件扩展名
    			$suffix = $request -> file('img') ->getClientOriginalExtension();
    			//随机一个文件名称
    			$fileName = time().mt_rand(100000,999999);
    			//移动文件
    			$request -> file('img') ->move('./uploads',$fileName.'.'.$suffix);

    			$data['img'] = $fileName.'.'.$suffix;

    			
    			
    		}
    	}
    	
    	//执行添加
    	$data['status'] =0;

    	$res = DB::table('goods') ->insert($data);
    	if($res)
    	{
    		return redirect('/admin/goods/index') ->with(['info' => '添加成功']);
    	}else
    	{
    		return back() ->with(['info' => '添加失败']);
    	}
    }


    public function getIndex(Request $request)
    {
    	$data = DB::table('goods')
    	->select('goods.*','cate.name AS cname')
    	->leftJoin('cate','goods.cate_id','=','cate.id')
    	 ->where('goods.name','like','%'.$request -> input('keywords').'%') 
         ->orderBy('goods.id')
    	 ->paginate($request ->input('num',10));

    	

    	return view('admin.goods.index',['data' => $data,'request' => $request ->all()]);
    }

   public function getEdit($id)
   {
   	$allData = DB::table('cate')
        ->select('*',DB::raw("CONCAT(path,',',id) AS sortPath"))
        ->orderby('sortPath')
        ->get();

        foreach($allData as $k=>$v)
        {   //获取path里','的个数
            //substr_repeat重复一个字符串
            $num = substr_count($v ->path,',');

            //利用','的个数去区分分类名
            $allData[$k] -> name = str_repeat('|----', $num).$v ->name;
        }

 	$data =DB::table('goods') ->where('id',$id) ->first();
 	return view('admin.goods.edit',['data' =>$data,'allData' =>$allData]);

   }

   public function postUpdate(Request $request)
   {
   	$data = $request -> except('_token');
   	
   	

   	if($request -> hasFile('img'))
    	{
    		 
    		if($request ->file('img') -> isValid())
    		{
    			//查询旧图片
    			$img = DB::table('goods') -> where('id',$data['id']) ->first()-> img;
    			//旧图片路径
    			$oldImg = './uploads/'.$img;

    			//移动文件
    			//创建文件名称
    			//获取文件扩展名
    			$suffix = $request -> file('img') ->getClientOriginalExtension();
    			//随机一个文件名称
    			$fileName = time().mt_rand(100000,999999);
    			//移动文件
    			$request -> file('img') ->move('./uploads',$fileName.'.'.$suffix);

    			$data['img'] = $fileName.'.'.$suffix;

    			if(file_exists($oldImg))
    			{
    				unlink($oldImg);
    			}

    			$data['img'] = $fileName.'.'.$suffix;
    			
    			
    			

    			
    			
    		}
    	}
    	
    	// 执行修改
    			$res = DB::table('goods') ->where('id',$data['id']) ->update($data);
				if($res)
		    	{
		    		return redirect('/admin/goods/index') ->with(['info' => '修改成功']);
		    	}else
		    	{
		    		return back() ->with(['info' => '修改失败']);
		    	}


   }

   public function getDelete($id)
   {
   	$img = DB::table('goods') -> where('id',$id) ->first() ->img;
   	$oldImg = './uploads/'.$img;
   	$res = DB::table('goods') ->delete($id);
   	if($res)
		{ 
			if(file_exists($oldImg))
			{
				unlink($oldImg);
			}
			
			return redirect('/admin/goods/index') ->with(['info' => '删除成功']);
		}else
		{
			return back() ->with(['info' => '删除失败']);
		}

   }
}
